package client_commands;

import java.util.StringTokenizer;

import client.ClientApplication;
import client.ClientBindObject;
import client_tree.User;

public class SERV_CHNICKCommand extends Command {

	
	public SERV_CHNICKCommand(String parameter) {
		setCode(SERV_CHNICK);
		setParameter(parameter);
	}
	
	
	@Override
	public boolean executeCommand(ClientApplication theApp) {

		StringTokenizer tokens = new StringTokenizer(getParameter());
		
		String username = tokens.nextToken();
		String newNickname = tokens.nextToken();

		User user = theApp.getClientModel().getOnlineModel().getUserByUsername(username);;
		
		if (user == null) {
			return false;
		}
		
		user.setNickname(newNickname);
		
		theApp.getWindow().refreshTree();
		
		ClientBindObject cbo = ClientApplication.clientBinding.get(user.getUsername());
		
		if (cbo != null) {
			if ( cbo.isDialogOpen() == true ) {
				cbo.getDialog().refreshTitle();
			}
		}
		
		return true;
	}
	
}
